package com.student.education.common;



import lombok.Data;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * 数据响应出参格式封装
 * @param <T>
 */

@Data
public class CommonResult<T> implements Serializable {

    /**
     * 成功标识；SUCCESS：成功；ERROR:失败
     */
    protected String code;

    /**
     * 描述信息
     */
    protected String msg;

    /**
     * 返回数据
     */
    protected T result;


    /**
     * 失败 不返回数据
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error() {
        CommonResult<T> responseMessage = new CommonResult<>();
        responseMessage.msg = "失败";
        responseMessage.code("ERROR");
        return responseMessage;
    }

    /**
     * 失败返回数据
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(String msg) {
        CommonResult<T> responseMessage = new CommonResult<>();
        responseMessage.msg = msg;
        responseMessage.code("ERROR");
        return responseMessage;
    }

    /**
     * 成功不返回数据
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success() {
        CommonResult<T> responseMessage = new CommonResult<>();
        responseMessage.msg = "成功";
        responseMessage.code("SUCCESS");
        return responseMessage;
    }

    /**
     * 成功返回提示
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(String msg) {
        CommonResult<T> responseMessage = new CommonResult<>();
        responseMessage.msg = msg;
        responseMessage.code("SUCCESS");
        return responseMessage;
    }

    /**
     * 成功返回提示和数据
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(String msg, T result) {
        CommonResult<T> responseMessage = new CommonResult<>();
        responseMessage.msg = msg;
        responseMessage.code("SUCCESS");
        if (result != null) {
            responseMessage.result = result;
        } else {
            responseMessage.result = (T) new LinkedHashMap<>();
        }
        return responseMessage;
    }


    public static <T> CommonResult<T> success(T result) {
        CommonResult<T> responseMessage = new CommonResult<>();
        responseMessage.msg = "成功";
        responseMessage.code("SUCCESS");
        if (result != null) {
            responseMessage.result = result;
        } else {
            responseMessage.result = (T) new LinkedHashMap<>();
        }
        return responseMessage;
    }


    public CommonResult<T> code(String code) {
        this.code = code;
        return this;
    }
}
